﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlcUI_test.MyControls
{

    [Description("三选一按钮控件")]
    public partial class ThreeRadioButton : UserControl
    {
        public ThreeRadioButton()
        {
            InitializeComponent();
        }
        private string text1;
        private string text2;
        private string text3;


        [Description("触发事件的按钮对应的参数")]
        public string ValueName { get; private set; } = "";

        [Description("第1个按钮对应的参数")]
        public string ValueName1 { get; set; } = "";
        [Description("第2个按钮对应的参数")]
        public string ValueName2 { get; set; } = "";
        [Description("第3个按钮对应的参数")]
        public string ValueName3 { get; set; } = "";
        [Description("第一个按钮对应的名称")]
        public string Text1
        {
            get { return text1; }
            set { text1 = value; radioButton1.Text = value;}
        }
        [Description("第二个按钮对应的名称")]
        public string Text2
        {
            get { return text2; }
            set { text2 = value; radioButton2.Text = value; }
        }
        [Description("第三个按钮对应的名称")]
        public string Text3
        {
            get { return text3; }
            set { text3 = value; radioButton3.Text = value; }
        }

        [Description("第四个按钮对应的名称")]
        public string Text4
        {
            get { return radioButton4.Text; }
            set { radioButton4.Text = value; }
        }

        public bool Button1Checked
        {
            get { return radioButton1.Checked; }
            set { radioButton1.Checked = value;}
        }
        public bool Button2Checked
        {
            get { return radioButton2.Checked; }
            set { radioButton2.Checked = value;}
        }
        public bool Button3Checked
        {
            get { return radioButton3.Checked; }
            set { radioButton3.Checked = value;}
        }

        public short ShortNumModel { get; private set; }

        public void Initialize() 
        {
            radioButton4.Checked = true;  // 默认右边选中
        }

        private void WhichButtonChecked()
        {
            
            if (radioButton1.Checked)
            {
                // Console.WriteLine(Text1 +" " + radioButton1.Checked);
                ValueName = ValueName1;
                ShortNumModel = 1;
                ButtonCheckedChanged?.Invoke(this, radioButton1.Checked);
                radioButton2.Checked = false;
                radioButton3.Checked = false;
            }
            else if (radioButton2.Checked)
            {
                // Console.WriteLine(Text2 + " " + radioButton2.Checked);
                ValueName = ValueName2;
                ShortNumModel = 2;
                ButtonCheckedChanged?.Invoke(this, radioButton2.Checked);
                radioButton1.Checked = false;
                radioButton3.Checked = false;
            }
            else if (radioButton3.Checked)
            {
                // Console.WriteLine(Text3 + " " + radioButton3.Checked);
                ValueName = ValueName3;
                ShortNumModel = 3;
                ButtonCheckedChanged?.Invoke(this, radioButton3.Checked);
                radioButton1.Checked = false;
                radioButton2.Checked = false;
            }
            
        }

        /// <summary>
        /// 这个地方只是事件的声明，不能加括号
        /// </summary>
        [Description("按钮值改变时触发")]
        public event EventHandler<bool> ButtonCheckedChanged;


        

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                WhichButtonChecked();
            
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
                WhichButtonChecked();
            
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
                WhichButtonChecked();
            
        }
    }
}
